using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Destra.Tools.DACGenerator.Config
{
	public class ClassConfig
	{
		[XmlAttribute("Namespace")]
		public string Namespace;

		[XmlAttribute("Class")]
		public string Class;

		public ClassConfig()
		{

		}
	}

	public class GeneratorConfig
	{
		[XmlElement("connectionString")]
		public string ConnectionString;

		[XmlElement("ClassConfig")]
		public ClassConfig ClassConfig;

        public GeneratorConfig()
		{

		}

		public static T Deserialize<T>(string xml) where T : GeneratorConfig
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			using (StringReader reader = new StringReader(xml))
			{
				return (T)serializer.Deserialize(reader);
			}
		}

		public static T Deserialize<T>(Stream xml) where T: GeneratorConfig
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));

			return (T)serializer.Deserialize(xml);
		}

		public string Serialize()
		{
			using (MemoryStream stream = new MemoryStream())
			{
				XmlSerializer serializer = new XmlSerializer(this.GetType());
				serializer.Serialize(stream, this);

				stream.Flush();
				stream.Seek(0, SeekOrigin.Begin);

				using (StreamReader reader = new StreamReader(stream))
				{
					return reader.ReadToEnd();
				}
			}
		}
	}	
}
